home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d20
/
msgq160s.arc
/
FIXAREAS.C
< prev
next >
Wrap
Text File
|
1991-10-26
|
4KB
|
217 lines
#include <stdio.h>
#include <string.h>
#include <dir.h>
#define NUMAREAS 256
#define STRLENGTH 300
#define FILENAMELEN 80
typedef
struct p_str{
char string[STRLENGTH];
struct p_str *next;
} p_str_type;
FILE *infile;
FILE *outfile;
int numarray[NUMAREAS];
char *p_strings;
int stringnum=0;
p_str_type *p_head;
p_str_type *this_p;
void error(char *errmsg)
{
printf("\nERROR: %s\n",errmsg);
exit(1);
}
char *change_extension(char *infile,
char *outfile)
{
char *p;
strcpy(outfile, infile);
p = strrchr(outfile, '.');
if (p != NULL)
*p = '\0';
strcat(outfile, ".bak");
}
void openfiles(char *outfilename)
{
char infilename[FILENAMELEN];
struct ffblk blk;
change_extension(outfilename,infilename);
if (findfirst(infilename, &blk, 0) == 0)
unlink(infilename);
rename(outfilename,infilename);
if ((infile = fopen(infilename,"r")) == NULL)
error("could not open input file");
if ((outfile = fopen(outfilename,"w")) == NULL)
error("could not open output file");
}
void initarray()
{
int x;
for (x=0;x<NUMAREAS;x++)
numarray[x] = 0;
}
int markarray(int num)
{
numarray[num] = 1;
}
int getlowest()
{
int x;
x = 1;
while ((numarray[x] != 0) && (x < NUMAREAS))
x++;
numarray[x] = 1;
return(x);
}
void storestr(char *str)
{
p_str_type *temp_p;
if (stringnum == 0)
{
p_head = (p_str_type *)malloc(sizeof(p_str_type));
if (p_head == NULL)
error("memory allocation error");
strcpy(p_head->string,str);
p_head->next = NULL;
this_p = p_head;
stringnum++;
}
else
{
temp_p = (p_str_type *)malloc(sizeof(p_str_type));
if (temp_p == NULL)
error("memory allocation error");
strcpy(temp_p->string,str);
temp_p->next = NULL;
this_p->next = temp_p;
this_p = this_p->next;
stringnum++;
}
}
void out_p_strings()
{
int x;
int num;
char numstr[STRLENGTH+5];
fputs("\n",outfile);
this_p = p_head;
for (x=0;x<stringnum;x++)
{
num = getlowest();
sprintf(numstr,"%03d%s",num,this_p->string);
fputs(numstr,outfile);
this_p = this_p->next;
}
}
void parse()
{
char filestr[STRLENGTH+1];
char parsestr[STRLENGTH+1];
char *savestr;
char ch;
int num;
while (!(feof(infile)))
{
if (fgets(filestr,STRLENGTH,infile) == NULL)
{
if (!(feof(infile)))
error("could not read input file");
}
else
if (sscanf(filestr," %[Pp;]",parsestr) == 1)
{
if (strlen(parsestr) != 0)
{
ch = parsestr[strlen(parsestr)-1];
savestr = strchr(filestr,ch);
strcpy(savestr,&savestr[1]);
if (ch == 'P')
storestr(savestr);
else
if (ch == ';')
{
num = atoi(savestr);
markarray(num);
if (fputs(parsestr,outfile) == EOF)
error("could not write to output file");
if (fputs(savestr,outfile) == EOF)
error("could not write to output file");
}
}
}
else
{
num = atoi(&filestr[0]);
markarray(num);
if (fputs(filestr,outfile) == EOF)
error("could not write to output file");
}
}
}
void closefiles()
{
if (fclose(infile) != 0)
error("could not close input file");
if (fclose(outfile) != 0)
error("could not close output file");
}
void p_copy()
{
initarray();
parse();
out_p_strings();
}
void main(int argc,
char *argv[])
{
char infilename[FILENAMELEN];
char outfilename[FILENAMELEN];
if (argc < 2)
{
printf(" usage: fixareas areas.bbs\n");
exit(1);
}
openfiles(argv[1]);
p_copy();
closefiles();
exit(0);
}